package org.meetunexpectedly.service.video.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.meetunexpectedly.entity.video.CommentThumb;
import org.meetunexpectedly.mapper.video.CommentThumbMapper;
import org.meetunexpectedly.service.video.CommentThumbService;
import org.springframework.stereotype.Service;

/**
 * @Description: 评论点赞服务实现类
 * @Author: pupil
 * @Date: 2025/1/20 下午 11:33
 */
@Service
public class CommentThumbServiceImpl extends ServiceImpl<CommentThumbMapper, CommentThumb> implements CommentThumbService{

    @Override
    public Boolean thumbUp(CommentThumb commentThumb) {
        try {
            // 添加点赞
            this.save(commentThumb);
        } catch (Exception e) {
            // 存在则取消点赞
            this.remove(new LambdaQueryWrapper<CommentThumb>().eq(CommentThumb::getCommentId, commentThumb.getCommentId()).eq(CommentThumb::getUserId, commentThumb.getUserId()));
            return false;
        }
        return true;
    }

    @Override
    public Boolean thumbUpState(Long commentId, Long userId) {
        if (userId == null) return false;
        // 点赞数为1则为已点赞  点赞数为0则未点赞
        return this.count(new LambdaQueryWrapper<CommentThumb>().eq(CommentThumb::getCommentId, commentId).eq(CommentThumb::getUserId, userId)) == 1;
    }
}
